﻿

using System.Collections.Generic;
using UnityEngine;
using SimpleJson;
using System;

namespace ABA
{
    public class GameTimer : Singleton<GameTimer>
    {
        Timer _timer;
        protected override void Init()
        {
            var root = GameObject.Find("SingletonManager");
            string instName = this.GetType().Name;
            var go = new GameObject(instName);
            if(root != null)
                go.transform.parent = root.transform;
            go.transform.position = Vector3.zero;
            
            if (Application.isPlaying)
            {
                UnityEngine.Object.DontDestroyOnLoad(go.transform.root);
            }     
            
            _timer = go.AddComponent<Timer>();       
        }
        public override void Release()
        {
            base.Release();
            GameObject.Destroy(_timer.gameObject);
            _timer = null;
        }

        // 每帧执行
        public long AddUpdate(TimerCallback update){
            return _timer.AddUpdate(update);            
        }

        // 只支持一次, delay 时间到了，会清除 finish, update
        public long AddCD_Update(float delay, TimerCallback finish, TimerCallback update){
            return _timer.AddCD_Update(delay, finish, update);             
        }

        // 添加定时器，id 为key  interval 单位s
        public long Add(float interval, int repeat, TimerCallback callback)
        {
            return _timer.Add(interval, repeat, callback);   
        }
        /**
         * @interval in seconds
         * @repeat 0 indicate loop infinitely, otherwise the run count
         **/
        public long Add(float interval, int repeat, TimerCallback callback, JsonObject callbackParam){
            return _timer.Add(interval, repeat, callback, callbackParam); 
        }
        public long CallLater(TimerCallback callback)
        {
            return _timer.CallLater(callback);
        }
        public long CallLater(TimerCallback callback, JsonObject callbackParam)
        {
            return _timer.CallLater(callback, callbackParam);
        }
        public bool Exists(long id)
        {
            return _timer.Exists(id);
        }
        // 移除定时器, 通过Add的时候的id
        public void Remove(long id)
        {
            _timer.Remove(id);
        }

        public void Pause(long id)
        {
            _timer.Pause(id);
        }

        public void Resume(long id)
        {
            _timer.Resume(id);
        }
    }
}




